#include <gtk/gtk.h>
#include <assert.h>

#include "common.h"

enum { COL_DISPLAY_NAME, COL_PIXBUF, NUM_COLS };

GtkTreeModel *init_model(void)
{
    GtkListStore *listStore;
    GdkPixbuf *p1, *p2, *p3, *p4;
    GtkTreeIter iter;
    GError *err = NULL;
    int i;

    p1 = gdk_pixbuf_new_from_file("image/fish.jpg", &err);  /*载入图标*/
    assert(err==NULL);
    p2 = gdk_pixbuf_new_from_file("image/flower.jpg", &err);
    assert(err==NULL);
    p3 = gdk_pixbuf_new_from_file("image/puppy.jpg", &err);
    assert(err==NULL);
    p4 = gdk_pixbuf_new_from_file("image/coffee.jpg", &err);
    assert(err==NULL);

    listStore = gtk_list_store_new(NUM_COLS, G_TYPE_STRING, GDK_TYPE_PIXBUF); /*数据储存*/

    for (i = 0; i < 50; ++i) {
        gtk_list_store_append(listStore, &iter);
        gtk_list_store_set(listStore, &iter, COL_DISPLAY_NAME, "fish", COL_PIXBUF, p1, -1);

        gtk_list_store_append(listStore, &iter);
        gtk_list_store_set(listStore, &iter, COL_DISPLAY_NAME, "flower", COL_PIXBUF, p2, -1);

        gtk_list_store_append(listStore, &iter);
        gtk_list_store_set(listStore, &iter, COL_DISPLAY_NAME, "puppy", COL_PIXBUF, p3, -1);

        gtk_list_store_append(listStore, &iter);
        gtk_list_store_set(listStore, &iter, COL_DISPLAY_NAME, "coffee", COL_PIXBUF, p4, -1);
    }
    return GTK_TREE_MODEL(listStore);
}

int main( int argc, char *argv[]){
    GtkWidget *window;
    GdkColor  color_ = {0x0, 0x50, 0x50, 0x50};
    GtkWidget *iconView;
    GtkWidget *sw;

    gtk_init(&argc, &argv);

    /* window */
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
#if 0
    gtk_window_fullscreen(GTK_WINDOW(window));              /*设置全屏*/
#else
    gtk_widget_set_size_request (window, 750, 550);         /*设置窗口大小*/
    gtk_window_set_resizable(GTK_WINDOW(window), FALSE);    /*设置窗口不能拉伸*/
#endif
    gtk_window_set_title(GTK_WINDOW(window), TITLE);
//    gtk_container_set_border_width(GTK_CONTAINER(window), 15);  /*设置容器边界大小*/
    gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color_); /*设置为背景颜色*/
//    gtk_window_set_decorated(GTK_WINDOW(window), FALSE);	/*设置不提供装饰*/

    /* scroll window */
    sw = gtk_scrolled_window_new(NULL, NULL);
    gtk_container_add(GTK_CONTAINER(window), sw);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_IN);

    /* icon view */
    iconView = gtk_icon_view_new_with_model(init_model());
    gtk_container_add(GTK_CONTAINER(sw), iconView);
    gtk_icon_view_set_text_column(GTK_ICON_VIEW(iconView), COL_DISPLAY_NAME);
    gtk_icon_view_set_pixbuf_column(GTK_ICON_VIEW(iconView), COL_PIXBUF);
    gtk_icon_view_set_selection_mode(GTK_ICON_VIEW(iconView), GTK_SELECTION_MULTIPLE);

    /* signal setting */
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}
